﻿/////////////////////////////////////////////////////////////////////////
//                                                                     //
//    MTGProject - A Magic The Gathering Networked Environment         //
//                                                                     //
//          Copyright (c) 2008-2009 by The MTGProject Team             //
//                                                                     //
// This file is part of MTGProject.                                    //
//                                                                     //
// MTGProject is free software; you can redistribute it and/or         //
// modify it under the terms of the GNU Lesser General Public          //
// License as published by the Free Software Foundation; either        //
// version 3 of the License, or (at your option) any later version.    //
//                                                                     //
// This library is distributed in the hope that it will be useful,     //
// but WITHOUT ANY WARRANTY; without even the implied warranty of      //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the       //
// GNU Lesser General Public License for more details.                 //
//                                                                     //
// You should have received a copy of the GNU Lesser General Public    //
// License along with this library; if not, write to the Free Software //
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA           //
// 02111-1307, USA, or contact the author(s):                          //
//                                                                     //
// Ruben Acuna <flyingfowlsoftware@earthlink.net>                      //
//                                                                     //
/////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace MTGProject
{
    class CardMaster
    {
        private static CardMaster uniqueInstance;

        LinkedList<ICard> tokenParents;
        LinkedList<ICard> cardParents;

        int cardsCreated = 0;

        private CardMaster()
        {
            tokenParents = new LinkedList<ICard>();
            cardParents = new LinkedList<ICard>();

            PopulateParents();
        }

        private void PopulateParents()
        {
            string[] cardXMLs = Directory.GetFiles(CardIO.cardPath);

            System.Console.Write("CardMaster: Loading cards... ");
            
            foreach (string card in cardXMLs)
            {
                if (card.Contains("token"))
                    tokenParents.AddLast(CardIO.LoadXMLCard(card, false));
                else
                    cardParents.AddLast(CardIO.LoadXMLCard(card, false));
            }

            System.Console.WriteLine("done.");
        }

        public static CardMaster getInstance()
        {
            if (uniqueInstance == null)
                uniqueInstance = new CardMaster();

            return uniqueInstance;
        }

        public bool IsToken(ICard card )
        {
            return tokenParents.Contains(card.Parent);
        }

        public ICard GetToken(CreatureType type)
        {
            foreach (ICard card in tokenParents)
            {
                if (card is CardCreature && ((CardCreature)card).Types.First.Value == type)
                    return CloneChildFromParent(card);
            }

            return null;
        }

        private ICard CloneChildFromParent(ICard parent)
        {
            cardsCreated++;

            if (parent is CardCreature)
            {
                CardCreature clonee = ((CardCreature)parent);
                CardCreature clone = new CardCreature(parent.Name, 
                                                      UtilMagic.CloneEnums<ManaColor>(parent.Colors),
                                                      UtilMagic.CloneEnums<ManaColor>(parent.Cost),
                                                      UtilMagic.CloneEnums<CreatureType>(clonee.Types),
                                                      UtilMagic.CloneEnums<CreatureKeyword>(clonee.Keywords),
                                                      clonee.Power, clonee.Toughness, parent.Script, parent.ImageURL, parent);

                return (ICard)clone;
            }
            else
                throw new Exception("CardMaster.CloneChildFromParent(): Encountered unknown parent type.");
        }
    }
}
